package com.platform.usercenter.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.platform.common.core.utils.StringUtils;
import com.platform.common.core.web.controller.BaseController;
import com.platform.common.core.web.domain.AjaxResult;
import com.platform.constants.SysUserConstants;
import com.platform.usercenter.domain.CalCalendar;
import com.platform.usercenter.domain.CalHoliday;
import com.platform.usercenter.domain.SysOrganizationRelation;
import com.platform.usercenter.service.ICalCalendarService;
import com.platform.usercenter.service.ICalHolidayService;
import com.platform.usercenter.service.ISysOrganizationRelationService;
import com.platform.usercenter.utils.CalendarUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 排班日历
 *
 * @author yinjinlu
 * @date 2022-06-10
 */
@RestController
@RequestMapping("/mes/cal/calendar")
public class CalCalendarController extends BaseController {

    @Resource
    private ICalCalendarService calCalendarService;

    @Resource
    private ICalHolidayService calHolidayService;

    @Resource
    ISysOrganizationRelationService organizationRelationService;

    @GetMapping("/list")
    public AjaxResult getCalendars( CalCalendar calCalendar){

        Date day = calCalendar.getDate();
        List<CalCalendar> days = null;
        if(StringUtils.isNull(day)){
            day = new Date();
        }
        String orgId=calCalendar.getOrgId( );
        if(Objects.isNull(orgId)){
            throw new RuntimeException( "orgId 查询参数缺失" );
        }else {
            //根据组织id查询下级组织的组织id
            // 查询下游组织信息-实际关系
            SysOrganizationRelation relationQuery = new SysOrganizationRelation();
            relationQuery.setParentId(Long.parseLong( orgId ));
            relationQuery.setRelation( SysUserConstants.ORG_REAL);
            relationQuery.setIsDelete(SysUserConstants.N);
            List<SysOrganizationRelation> realOrgList = organizationRelationService.selectSysOrganizationRelationList(relationQuery);
            if (CollectionUtil.isNotEmpty(realOrgList)) {
                List< Long > collect=realOrgList.stream( ).map( SysOrganizationRelation::getOrgId )
                        .collect( Collectors.toList( ) );
                collect.add( Long.parseLong( orgId ) );
                calCalendar.setOrgIdList( collect );
                calCalendar.setOrgId(null);
            }
        }
        if(SysUserConstants.CAL_QUERY_BY_TYPE.equals(calCalendar.getQueryType())){
            days=calCalendarService.getCalendarByType(day,calCalendar.getCalendarType(),calCalendar);
        }else if(SysUserConstants.CAL_QUERY_BY_TEAM.equals(calCalendar.getQueryType())){
            days=calCalendarService.getCalendarByTeam(day,calCalendar.getTeamId(),calCalendar);
        }else {
            days=calCalendarService.getCalendarByUser(day,calCalendar.getUserId(),calCalendar);
        }
        return AjaxResult.success(getCalendarsWithoutHoliday(days,calCalendar));
    }



    /**
     * 过滤掉节假日
     * @param days
     * @return
     */
    private List<CalCalendar> getCalendarsWithoutHoliday(List<CalCalendar> days,CalCalendar dar){
        CalHoliday param = new CalHoliday();
        String orgId=dar.getOrgId( );
        if(StringUtils.isNotEmpty(  orgId)){
            param.setOrgId(Long.parseLong( dar.getOrgId() ) );
        }
        param.setOrgIdList( dar.getOrgIdList() );
        List<CalHoliday> holidays = calHolidayService.selectCalHolidayList(param);
        if(CollUtil.isNotEmpty(holidays) && days !=null && days.size()>0){
            List<CalCalendar> results = days.stream().
                    filter(calCalendar -> holidays.stream().
                            filter(calHoliday -> calCalendar.getTheDay().equals( CalendarUtil.getDateStr(calHoliday.getTheDay())) && SysUserConstants.HOLIDAY_FREE.equals( calHoliday.getHolidayType() ) ).collect(Collectors.toList()).size()==0
            ).collect(Collectors.toList());
            return results;
        }else {
            return days;
        }
    }
}
